package com.example.mq1.mqserver.datacenter;

import com.example.mq1.Mq1Application;
import com.example.mq1.mqserver.core.Binding;
import com.example.mq1.mqserver.core.Exchange;
import com.example.mq1.mqserver.core.ExchangeType;
import com.example.mq1.mqserver.core.MSGQueue;
import com.example.mq1.mqserver.mapper.MetaMapper;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class DataBaseManager {

    private MetaMapper metaMapper;

    /**
     * 初始化方法，建库建表
     * 不用构造方法的原因是因为有一些业务逻辑在内
     */
    public void init() {
        //手动获取数据库
        metaMapper = Mq1Application.context.getBean(MetaMapper.class);
        //1.检查数据库文件是否存在,不存在就创建
        if(!checkDBExists()) {
            //先创建出文件目录(数据库操作表时自动创建，如果没有这个目录就会报错)
            File DirFile = new File("./data");
            DirFile.mkdir();
            //建表
            createTable();
            //插入一个默认数据(RabbitMQ 默认会插入一个 匿名的直接交换机数据)
            insertDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成！");
        } else {
            System.out.println("[DataBaseManager] 数据库数据存在！");
        }
    }

    private void insertDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setExchangeName("");
        exchange.setExchangeType(ExchangeType.DRICT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成");
    }

    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成！");
    }

    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

    /**
     * 将数据库操作也一并封装
     */
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectExchangeAll() {
        return metaMapper.selectExchangeAll();
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public List<MSGQueue> selectQueueAll() {
        return metaMapper.selectQueueAll();
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectBindingAll() {
        return metaMapper.selectBindingAll();
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public void deleteDB() throws IOException {
        //直接删除目录文件即可
        File Datafile = new File("./data/meta.db");
        boolean ok = Datafile.delete();
        if(ok) {
            System.out.println("[DataBaseManager] 数据库文件删除成功！");
        } else {
            System.out.println("[DataBaseManager] 数据库文件删除失败！");
        }
        File Dirfile = new File("./data");
        ok = Dirfile.delete();
        if(ok) {
            System.out.println("[DataBaseManager] 数据库目录删除成功！");
        } else {
            System.out.println("[DataBaseManager] 数据库目录删除失败！");
        }
    }


}
